﻿using CarAppClient.Consts;
using CarAppClient.Controls.MessageUsercontrols;
using CarAppClient.Extension;
using CarAppClient.Helpers;
using CarAppClient.ViewModels;
using CarAppClient.Views.CarType.Views;
using CarAppDto.CarType.CountryDtos;
using CarAppDto.CarType.CarDtos;
using D.UtilCore.Dtos;
using CarAppDto.CarType.AreaDtos;
using System.Collections.ObjectModel;
using System.Xml.Linq;
using Autofac.Features.OwnedInstances;
using Newtonsoft.Json;

namespace CarAppClient.Views.CarType.ViewModels.CarType
{
    public class AreaQueryViewModel : BaseQueryViewModel<Page_Area_Query, RepArea>
    {
        #region 加载页面数据     
        RepArea _repArea;

        Page_Area_Query _owner;
        public AreaQueryViewModel(Page_Area_Query owner, string? type) : base(owner, type)
        {
            _owner = owner;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="repCarType"></param>
        protected async override void LoadData()
        {
            Data = new ObservableCollection<RepArea>();
            Query();
        }

        #region 查询条件

        private string? name;
        /// <summary>
        /// 名称
        /// </summary>
        public string? Name
        {
            get { return name; }
            set { name = value; RaisePropertyChanged(); }
        }

        #endregion
        #region 查询


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="repCarType"></param>
        protected override async void Query(object? obj = null)
        {
            if (Data != null)
            {
                Data.Clear();
            }
            else
            {
                Data = new ObservableCollection<RepArea>();
            }

            // 请求后端接口 
            var res = await HttpHelper<List<RepArea>>.PostAsync(ApiUrl.QueryAreaList, new RepArea()
            {
                Name = Name
            });

            //  判断结果
            if (res.Code == StatusCodeEnum.Waring)
            {
                MsgUtil.Warning(res.ErrorMsg);
            }
            else if (res.Code == StatusCodeEnum.Error)
            {
                MsgUtil.Error(res.ErrorMsg);
            }
            else if (res.Code == StatusCodeEnum.ServerError)
            {
                MsgUtil.Error("服务器异常");
            }
            else if (res.Code == StatusCodeEnum.OK)
            {
                // 绑定数据源到UI 
                if (res.Data != null)
                {
                    foreach (var item in res.Data)
                    {
                        item.IsSelected = false;
                        Data.Add(item);
                    }
                }
            }
            else
            {
                MsgUtil.Error(res.Code.ToString());
            }
        }
        #endregion
        #endregion

        // 事件
        public event InformationRequestedHandler InformationRequested;
        protected override async void Submit(object? obj = null)
        {
            if (obj == null) return;
            List<RepArea> listArea =JsonConvert.DeserializeObject<List<RepArea>>( JsonConvert.SerializeObject(obj));
            if(listArea.FindAll(a=>a.IsSelected==true).Count()>1)
            {
                MsgUtil.Error("只能选择一个");
                return;
            }
            if (listArea.FindAll(a => a.IsSelected == true).Count()==0)
            {
                MsgUtil.Error("请选择一个");
                return;
            }
            _repArea = listArea.FirstOrDefault(a=>a.IsSelected==true);       
            _owner.Close();
            InformationRequested?.Invoke(this, new InformationEventArgs<RepArea>(_repArea));
        }
    }
   
}
